1
2
3
4
5
6
7
8
9
10
11
12
13 package org.apache.tapestry5.internal.services;
14
15 import org.apache.tapestry5.internal.test.InternalBaseTestCase;
16 import org.apache.tapestry5.services.Request;
17 import org.apache.tapestry5.services.Session;
18 import org.testng.annotations.DataProvider;
19 import org.testng.annotations.Test;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpSession;
23 import java.io.UnsupportedEncodingException;
24 import java.util.Collections;
25
26 public class RequestImplTest extends InternalBaseTestCase
27 {
28 public static final String CHARSET = "UTF-8";
29
30
31 @Test
32 public void get_session_doesnt_exist()
33 {
34 HttpServletRequest sr = mockHttpServletRequest();
35 TapestrySessionFactory sf = newMock(TapestrySessionFactory.class);
36
37 expect(sf.getSession(false)).andReturn(null);
38
39 replay();
40
41 Request request = new RequestImpl(sr, CHARSET, sf);
42
43 assertNull(request.getSession(false));
44
45 verify();
46 }
47
48 @Test
49 public void force_session_create()
50 {
51 HttpServletRequest sr = mockHttpServletRequest();
52 TapestrySessionFactory sf = newMock(TapestrySessionFactory.class);
53 Session session = mockSession();
54
55 expect(sf.getSession(true)).andReturn(session);
56
57 replay();
58
59 Request request = new RequestImpl(sr, CHARSET, sf);
60
61 assertSame(request.getSession(true), session);
62
63 verify();
64 }
65
66 @Test
67 public void used_encoding_from_request() throws Exception {
68 HttpServletRequest sr = mockHttpServletRequest();
69
70 expect(sr.getCharacterEncoding()).andReturn("request-encoding");
71
72 sr.setCharacterEncoding("request-encoding");
73
74 expect(sr.getParameterNames()).andReturn(Collections.enumeration(Collections.EMPTY_LIST));
75
76 replay();
77
78 new RequestImpl(sr, "app-encoding-is-ignored", null).getParameterNames();
79
80 verify();
81 }
82
83 @Test
84 public void set_encoding_success() throws Exception
85 {
86 HttpServletRequest sr = mockHttpServletRequest();
87
88 String encoding = "the-encoding";
89
90 sr.setCharacterEncoding(encoding);
91
92 expect(sr.getCharacterEncoding()).andReturn(null);
93
94 expect(sr.getParameterNames()).andReturn(Collections.enumeration(Collections.EMPTY_LIST));
95
96 replay();
97
98 new RequestImpl(sr, encoding, null).getParameterNames();
99
100 verify();
101 }
102
103 @Test
104 public void set_encoding_failure() throws Exception
105 {
106 HttpServletRequest sr = mockHttpServletRequest();
107
108 String encoding = "the-encoding";
109 UnsupportedEncodingException exception = new UnsupportedEncodingException("Oops.");
110
111 sr.setCharacterEncoding(encoding);
112 setThrowable(exception);
113
114 expect(sr.getCharacterEncoding()).andReturn(null);
115
116 replay();
117
118 try
119 {
120 new RequestImpl(sr, encoding, null).getParameterNames();
121 unreachable();
122 } catch (RuntimeException ex)
123 {
124 assertSame(ex.getCause(), exception);
125 }
126
127 verify();
128 }
129
130 @Test(dataProvider = "xhr_inputs")
131 public void is_xhr_request(String headerValue, boolean expected)
132 {
133 HttpServletRequest sr = mockHttpServletRequest();
134
135 expect(sr.getHeader(RequestImpl.REQUESTED_WITH_HEADER)).andReturn(headerValue);
136
137 replay();
138
139 Request request = new RequestImpl(sr, CHARSET, null);
140
141 assertEquals(request.isXHR(), expected);
142
143 verify();
144 }
145
146 @DataProvider
147 public Object[][] xhr_inputs()
148 {
149 return new Object[][]
150 {
151 {null, false},
152 {"", false},
153 {"some other value", false},
154 {"XMLHttpRequest", true}};
155 }
156
157 @Test
158 public void get_path_for_normal_servlet_container()
159 {
160 String path = "/foo/bar";
161
162 HttpServletRequest sr = mockHttpServletRequest();
163
164 train_getPathInfo(sr, null);
165 expect(sr.getServletPath()).andReturn(path);
166
167 replay();
168
169 Request request = new RequestImpl(sr, CHARSET, null);
170
171 assertEquals(request.getPath(), path);
172
173 verify();
174 }
175
176
177
178
179 @Test
180 public void get_path_for_websphere_with_empty_path()
181 {
182 String path = "/foo/bar";
183
184 HttpServletRequest sr = mockHttpServletRequest();
185
186 train_getPathInfo(sr, path);
187
188 replay();
189
190 Request request = new RequestImpl(sr, CHARSET, null);
191
192 assertEquals(request.getPath(), path);
193
194 verify();
195 }
196
197
198
199
200 @Test
201 public void get_path_for_websphere_with_nonempty_path()
202 {
203 HttpServletRequest sr = mockHttpServletRequest();
204
205 train_getPathInfo(sr, "");
206
207 replay();
208
209 Request request = new RequestImpl(sr, CHARSET, null);
210
211 assertEquals(request.getPath(), "/");
212
213 verify();
214 }
215
216
217 protected final void train_getPathInfo(HttpServletRequest request, String pathInfo)
218 {
219 expect(request.getPathInfo()).andReturn(pathInfo).atLeastOnce();
220 }
221
222 @Test
223 public void isSessionInvalidated_is_false_when_no_session_at_all()
224 {
225 HttpServletRequest sr = mockHttpServletRequest();
226
227 TapestrySessionFactory sf = newMock(TapestrySessionFactory.class);
228
229 expect(sf.getSession(false)).andReturn(null);
230
231 replay();
232
233 Request request = new RequestImpl(sr, CHARSET, sf);
234
235 assertFalse(request.isSessionInvalidated());
236
237 verify();
238 }
239
240 @Test
241 public void isSessionInvalidated_is_false_when_session_exists_and_is_valid()
242 {
243 HttpServletRequest sr = mockHttpServletRequest();
244 Session session = mockSession();
245
246 TapestrySessionFactory sf = newMock(TapestrySessionFactory.class);
247
248 expect(sf.getSession(false)).andReturn(session);
249 expect(session.isInvalidated()).andReturn(false);
250
251 replay();
252
253 Request request = new RequestImpl(sr, CHARSET, sf);
254
255 assertFalse(request.isSessionInvalidated());
256
257 verify();
258 }
259
260 @Test
261 public void isSessionInvalidated_is_true_when_session_is_invalid()
262 {
263 HttpServletRequest sr = mockHttpServletRequest();
264 Session session = mockSession();
265
266 TapestrySessionFactory sf = newMock(TapestrySessionFactory.class);
267
268 expect(sf.getSession(false)).andReturn(session);
269 expect(session.isInvalidated()).andReturn(true);
270
271 replay();
272
273 Request request = new RequestImpl(sr, CHARSET, sf);
274
275 assertTrue(request.isSessionInvalidated());
276
277 verify();
278 }
279
280 @Test
281 public void request_secure_with_x_forwarded_proto() throws Exception
282 {
283 HttpServletRequest sr = mockHttpServletRequest();
284
285 expect(sr.isSecure()).andReturn(false);
286 expect(sr.getHeader(RequestImpl.X_FORWARDED_PROTO_HEADER)).andReturn("https");
287
288 replay();
289
290 Request request = new RequestImpl(sr, CHARSET, null);
291
292 assertTrue(request.isSecure());
293
294 verify();
295 }
296 }